package com.sunwayworld.basemodule.coredata.test.service.impl;

import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodAnalyteBean;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodBean;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodEquipmentBean;
import com.sunwayworld.basemodule.coredata.test.bean.LimsTestMethodMaterialBean;
import com.sunwayworld.basemodule.coredata.test.dao.LimsTestMethodDao;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodAnalyteService;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodEquipmentService;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodMaterialService;
import com.sunwayworld.basemodule.coredata.test.service.LimsTestMethodService;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Repository
@GikamBean
public class LimsTestMethodServiceImpl implements LimsTestMethodService {

    @Autowired
    private LimsTestMethodDao limsTestMethodDao;

    @Autowired
    @Lazy
    private LimsTestMethodAnalyteService analyteService;

    @Autowired
    @Lazy
    private LimsTestMethodEquipmentService equipmentService;

    @Autowired
    @Lazy
    private LimsTestMethodMaterialService materialService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsTestMethodDao getDao() {
        return limsTestMethodDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        List<LimsTestMethodBean> parse = jsonWrapper.parse(LimsTestMethodBean.class);
        parse.forEach(e -> e.setId(ApplicationContextHelper.getNextIdentity()));
        getDao().insert(parse);
        return parse.get(0).getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsTestMethodBean> methodBeans = wrapper.parse(LimsTestMethodBean.class);
        if(CollectionUtils.isEmpty(methodBeans)) return;
        // 级联删除
        List<LimsTestMethodAnalyteBean> analyteBeans = new ArrayList<>();
        List<LimsTestMethodEquipmentBean> equipmentBeans = new ArrayList<>();
        List<LimsTestMethodMaterialBean> materialBeans = new ArrayList<>();
        methodBeans.forEach(e -> {
            LimsTestMethodAnalyteBean testBean = new LimsTestMethodAnalyteBean();
            testBean.setTestMethodId(e.getId());
            analyteBeans.add(testBean);
            LimsTestMethodMaterialBean methodBean = new LimsTestMethodMaterialBean();
            methodBean.setTestMethodId(e.getId());
            materialBeans.add(methodBean);
            LimsTestMethodEquipmentBean equipmentBean = new LimsTestMethodEquipmentBean();
            equipmentBean.setTestMethodId(e.getId());
            equipmentBeans.add(equipmentBean);
        });
        analyteService.getDao().deleteBy(analyteBeans, "testMethodId");
        equipmentService.getDao().deleteBy(equipmentBeans, "testMethodId");
        materialService.getDao().deleteBy(materialBeans, "testMethodId");
        getDao().deleteBy(methodBeans);
    }
}
